package com.ytd.common.util;

import android.text.TextUtils;

import com.ytd.common.bean.DownloadModel;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class YtdConfig {
    public static final String DevSecretKey =
            "xGmFEL3vkuNWxw54a3JbNwZdCmpCaw1aZluHWwcr6PA4aBY3LkHXcNJWeRw05/LXRU2xgL+UpMcRQGrLzFU6RqejtcLqVOW4ZVMRp6eWnA4=";
    public static final String SecretKey =
            "p2Un0npf/u1tTGT0fQmbTFr6B9E743xs0+j3/XO3hiMuB9shr0q4FmqxftS9523mpKVNEy9NIwO5FoQc98Ixv8rFubcASvfRyKWhlAEB2Eg=";

    public static final String rsaPrivateKey = "";
    public static final String rsaPublicKey = "";
    public static final String merchantName = "";
    public static final String merchantPwd = "";

    public static final int heartBeatTime = 30;
    public static final int reconnectTime = 60;
    public static final boolean offlineMode = true;
    public static final String initialName = "激活";
    public static String Token;
    public static DownloadModel model;
    public static final String DEFAULT_IP = "uat.bjytsk.com";
    public static final int DEFAULT_PORT = 7170;
    public static final int DEFAULT_ROW = 1;
    public static final int DEFAULT_COLUMN = 1;
    //非营业时间是否允许消费：0-否，1-是
    public static final int CONSUME_NON_BUSINESS_ENABLED_DEF = 1;
    //是否启用单次消费限额 0-否，1-是
    public static final int CONSUME_LIMIT_ONCE_ENABLED_DEF = 0;
    //是否启用定时开关机：0-否，1-是
    public static final int SCHEDULE_SWITCH_ENABLED_DEF = 0;

    public static final String DEVICE_ACTIVATE = "deviceActivate";

    public static Boolean isDeviceActivate;

    public static final String ConsumeModeDef = "0";
    //是否启用定时开关机：0-否，1-是
    public static final String ConsumeModeOrder = "1";

    public static String sConsumeMode = null;

    public static boolean isDeviceActivate() {
        if (isDeviceActivate == null) {
            isDeviceActivate = SPUtils.getInstance().getBoolean(DEVICE_ACTIVATE, false);
        }
        return isDeviceActivate;
    }

    public static boolean initBaseKey() {
        String ss = "";
        try {
            File file = new File(FileUtils.KeyPath);
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
            BufferedReader br = new BufferedReader(isr);
            String key = "";
            while ((key = br.readLine()) != null) {
                ss = ss + key;
            }
            if (TextUtils.isEmpty(ss)) {
                return false;
            }
            YtdConfig.setSecretKey(ss);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static void writeKey() {
        try {
            File file = new File(FileUtils.KeyPath);
            BufferedWriter bw = new BufferedWriter(new FileWriter(file, false));
            bw.write(SecretKey);
            bw.flush();
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            File file = new File(FileUtils.DevKeyPath);
            BufferedWriter bw = new BufferedWriter(new FileWriter(file, false));
            bw.write(DevSecretKey);
            bw.flush();
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void setDeviceActivate(boolean deviceActivate) {
        isDeviceActivate = deviceActivate;
        SPUtils.getInstance().putBoolean(DEVICE_ACTIVATE, deviceActivate);
    }

    public static String getSecretKey() {
        return SPUtils.getInstance().getString("secretKey", "");
    }

    public static void setSecretKey(String key) {
        SPUtils.getInstance().putString("secretKey", key);
    }

    public static String getRsaPrivateKey() {
        return SPUtils.getInstance().getString("rsaPrivateKey", rsaPrivateKey);
    }

    public static void setRsaPrivateKey(String rsaPrivateKey) {
        SPUtils.getInstance().putString("rsaPrivateKey", rsaPrivateKey);
    }

    public static String getMerchantName() {
        return SPUtils.getInstance().getString("merchantName", merchantName);
    }

    public static void setMerchantName(String merchantName) {
        SPUtils.getInstance().putString("merchantName", merchantName);
    }

    public static String getMerchantPwd() {
        return SPUtils.getInstance().getString("merchantPwd", merchantPwd);
    }

    public static void setMerchantPwd(String merchantPwd) {
        SPUtils.getInstance().putString("merchantPwd", merchantPwd);
    }

    public static String getRsaPublicKey() {
        return SPUtils.getInstance().getString("rsaPublicKey", rsaPublicKey);
    }

    public static void setRsaPublicKey(String rsaPublicKey) {
        SPUtils.getInstance().putString("rsaPublicKey", rsaPublicKey);
    }

    public static String getToken() {
        return Token;
    }

    public static void setToken(String token) {
        Token = token;
    }

    public static String getIP() {
        return SPUtils.getInstance().getString("IP", DEFAULT_IP);
    }

    public static void setIP(String ip) {
        SPUtils.getInstance().putString("IP", ip);
    }

    public static int getPort() {
        return SPUtils.getInstance().getInt("PORT", DEFAULT_PORT);
    }

    public static void setPort(int Port) {
        SPUtils.getInstance().putInt("PORT", Port);
    }

    public static int getRow() {
        return SPUtils.getInstance().getInt("ROW", DEFAULT_ROW);
    }

    public static void setRow(int Row) {
        SPUtils.getInstance().putInt("ROW", Row);
    }

    public static int getColumn() {
        return SPUtils.getInstance().getInt("COLUMN", DEFAULT_COLUMN);
    }

    public static void setColumn(int column) {
        SPUtils.getInstance().putInt("COLUMN", column);
    }

    public static int getHeartBeatTime() {
        return SPUtils.getInstance().getInt("heartBeatTime", heartBeatTime);
    }

    public static void setHeartBeatTime(int time) {
        SPUtils.getInstance().putInt("heartBeatTime", time);
    }

    public static DownloadModel getModel() {
        return model;
    }

    public static void setModel(DownloadModel model) {
        YtdConfig.model = model;
    }

    public static void setReconnectTime(int Time) {
        SPUtils.getInstance().putInt("reconnectTime", Time);
    }

    public static void setOfflineMode(String mode) {
        if (mode.equals("auto")) {
            SPUtils.getInstance().putBoolean("offlineMode", true);
        } else {
            SPUtils.getInstance().putBoolean("offlineMode", false);
        }
    }

    public static Boolean getOfflineMode() {
        return SPUtils.getInstance().getBoolean("offlineMode", offlineMode);
    }

    /**
     * 1-刷卡，2-人脸，3-刷卡或人脸
     *
     * @param bindingMode 刷卡模式
     */
    public static void setBindingMode(Integer bindingMode) {
        SPUtils.getInstance().putInt("bindingMode", bindingMode);
    }

    public static int getBindingMode() {
        return SPUtils.getInstance().getInt("bindingMode", 3);
    }

    public static int getReconnectTime() {
        return SPUtils.getInstance().getInt("reconnectTime", reconnectTime);
    }

    public static String getInitialName() {
        return SPUtils.getInstance().getString("initialName", initialName);
    }

    public static void setInitialName(String name) {
        SPUtils.getInstance().putString("initialName", name);
    }

    public static String getFileUploadUrl() {
        return SPUtils.getInstance().getString("fileUploadUrl", "");
    }

    public static void setFileUploadUrl(String fileUploadUrl) {
        SPUtils.getInstance().putString("fileUploadUrl", fileUploadUrl);
    }

    public static int getAlarmThreshold() {
        return SPUtils.getInstance().getInt("alarmThreshold", reconnectTime);
    }

    public static void setAlarmThreshold(int threshold) {
        SPUtils.getInstance().putInt("alarmThreshold", threshold);
    }

    public static String getShowVersion() {
        return SPUtils.getInstance().getString("showVersion", "");
    }

    public static void setShowVersion(String showVersion) {
        SPUtils.getInstance().putString("showVersion", showVersion);
    }

    public static String getMerchantId() {
        return SPUtils.getInstance().getString("merchantId", "");
    }

    public static void setMerchantId(String merchantId) {
        SPUtils.getInstance().putString("merchantId", merchantId);
    }

    public static String getPosNo() {
        return SPUtils.getInstance().getString("posNo", "");
    }

    public static void setPosNo(String posNo) {
        SPUtils.getInstance().putString("posNo", posNo);
    }

    public static double getConsumeLimitOnceAmount() {
        return SPUtils.getInstance().getFloat("consumeLimitOnceAmount", 0.0f);
    }

    public static void setConsumeLimitOnceAmount(float consumeLimitOnceAmount) {
        SPUtils.getInstance().putFloat("consumeLimitOnceAmount", consumeLimitOnceAmount);
    }

    public static boolean getConsumeLimitOnceEnabled() {
        return SPUtils.getInstance().getInt("consumeLimitOnceEnabled", CONSUME_LIMIT_ONCE_ENABLED_DEF) == 1;
    }

    public static void setConsumeLimitOnceEnabled(int consumeLimitOnceEnabled) {
        SPUtils.getInstance().putInt("consumeLimitOnceEnabled", consumeLimitOnceEnabled);
    }

    public static boolean getConsumeNonBusinessEnabled() {
        return SPUtils.getInstance().getInt("consumeNonBusinessEnabled", CONSUME_NON_BUSINESS_ENABLED_DEF) == 1;
    }

    public static void setConsumeNonBusinessEnabled(int consumeNonBusinessEnabled) {
        SPUtils.getInstance().putInt("consumeNonBusinessEnabled", consumeNonBusinessEnabled);
    }

    public static boolean getScheduleSwitchEnabled() {
        return SPUtils.getInstance().getInt("scheduleSwitchEnabled", SCHEDULE_SWITCH_ENABLED_DEF) == 1;
    }

    public static void setScheduleSwitchEnabled(int scheduleSwitchEnabled) {
        SPUtils.getInstance().putInt("scheduleSwitchEnabled", scheduleSwitchEnabled);
    }


    public static long getStartTime() {
        return SPUtils.getInstance().getLong("startTime", 0L);
    }

    public static void setStartTime(long startTime) {
        SPUtils.getInstance().putLong("startTime", startTime);
    }

    public static String getDownloadUrl() {
        return SPUtils.getInstance().getString("DownloadUrl", "");
    }

    public static void setDownloadUrl(String downloadUrl) {
        SPUtils.getInstance().putString("DownloadUrl", downloadUrl);
    }

    public static void setFaceKey(String faceKey) {
        SPUtils.getInstance().putString("faceKey", faceKey);
    }

    public static String getFaceKey() {
        return SPUtils.getInstance().getString("faceKey", "");
    }

    public static String getShutdownLocal() {
        return SPUtils.getInstance().getString("ShutdownLocal", "");
    }

    public static void setShutdownLocal(String shutdownLocal) {
        SPUtils.getInstance().putString("ShutdownLocal", shutdownLocal);
    }

    public static String getConsumeMode() {
        if (sConsumeMode == null) {
            sConsumeMode = SPUtils.getInstance().getString("ConsumeMode", "0");
        }
        return sConsumeMode;
    }

    public static void setConsumeMode(String consumeMode) {
        sConsumeMode = consumeMode;
        SPUtils.getInstance().putString("ConsumeMode", consumeMode);
    }
}
